package com.example.test;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author xiejp on 2022/4/10
 * 题目:请举例说明集合类是不安全的
 * 1 ArrayList 线程不安全
 * 1.1 使用Vector
 * 1.2 Collections.synchronizedList()
 * 1.3 CopyOnWriteArrayList  写时复制
 */

public class NotSafeDemo {
    public static void main(String[] args) {
        listNotSafe();
    }

    private static void listNotSafe() {
        List<String> list = new CopyOnWriteArrayList<>();
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }
}
